
#include "ResponseHeader.h"
#include <header.h>
#include "Constants.h"

ResponseHeader* createResponseHeader(unsigned short flag, const char *data) {
    // 为结构体分配内存
    ResponseHeader *header = (ResponseHeader*)calloc(1, sizeof(ResponseHeader));
    if (header == NULL) {
        perror("Failed to allocate memory");
        return NULL;
    }

    // 初始化结构体成员
    header->flag = flag;
    strncpy(header->data, data, BUFF_SIZE - 1);
    // 确保字符串以 null 结尾
    header->data[BUFF_SIZE - 1] = '\0';

    return header;
}

void freeResponseHeader(ResponseHeader *header) {
    if (header == NULL) return;

    // 释放结构体本身的内存
    free(header);
}

int recvResponseHeader(int net_fd, ResponseHeader *header){
    return recv(net_fd, header, sizeof(ResponseHeader), MSG_WAITALL);
}
